package com.service;

import com.mapper.ProductMapper;
import com.mapper.ShopCartMapper;
import com.pojo.ConsumerProductRelationshipRel;
import com.pojo.Product;
import com.pojo.view.ShopCartView;
import com.service.iter.IProductsService;
import com.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 实施产品服务
 *
 * @author 蔡文欣
 * @date 2023/06/24 09:43:50
 */
@Service
@Transactional
public class ProductsServiceImpl implements IProductsService {


    /**
     * 产品映射器
     **/
    @Autowired
    private ProductMapper productMapper;

    /**
     * 购物车映射器
     **/
    @Autowired
    private ShopCartMapper shopCartMapper;

    /**
     * 获取产品列表
     *
     * @param pageNo 页码
     * @return {@link List}<{@link Product}>
     */
    @Override
    public List<Product> getProducts(Integer pageNo, String query) {
        pageNo = (pageNo - 1) * 9;
        List<Product> products = productMapper.getProducts(pageNo, query);
        for (Product product : products) {
            Product maxPrice = productMapper.getProductMax_minPrice(product.getProductId(), "max");
            Product minPrice = productMapper.getProductMax_minPrice(product.getProductId(), "min");

            if (!Objects.equals(null, minPrice)) {
                product.setProductMinPrice(minPrice.getProductMinPrice());
            }

            if (!Objects.equals(null, maxPrice)) {
                product.setProductMaxPrice(maxPrice.getProductMaxPrice());

            }
        }
        return products;
    }

    /**
     * 获取产品价格
     *
     * @param id     ID
     * @param sizeId 尺寸id。
     * @return {@link Product}
     */
    @Override
    public Product getProductPrice(Long id, Integer sizeId) {
        Product productPrice = productMapper.getProductPrice(id, sizeId);
        if (Objects.equals(null, productPrice)) {
            return null;
        }
        System.out.println(productPrice);
        return productPrice;
    }

    /**
     * 按ID获取产品
     *
     * @param id ID
     * @return {@link Product}
     */
    @Override
    public Product byIdGetProduct(Long id) {
        Product product = productMapper.byIdGetProduct(id);
        if (Objects.equals(null, product)) {
            return null;
        }
        Product maxPrice = productMapper.getProductMax_minPrice(id, "max");

        Product minPrice = productMapper.getProductMax_minPrice(id, "min");

        if (!Objects.equals(null, minPrice)) {
            product.setProductMinPrice(minPrice.getProductMinPrice());
        }
        if (!Objects.equals(null, maxPrice)) {
            product.setProductMaxPrice(maxPrice.getProductMaxPrice());
        }
        return product;
    }

    /**
     * 通过消费者电话获取商店
     *
     * @param consumerPhone 消费者电话
     * @return {@link List}<{@link ShopCartView}>
     */
    @Override
    public List<ShopCartView> byConsumerPhoneGetShops(String consumerPhone) {
        return shopCartMapper.byConsumerPhoneGetShops(consumerPhone);
    }

    /**
     * 添加购物车
     *
     * @param component 组件
     * @return {@link Res}
     */
    @Override
    public Res addShopCart(ConsumerProductRelationshipRel component) {
        Res res = new Res();
        Long productId = component.getProductId();
        Long sizeId = component.getSizeId();
        String consumerPhone = component.getConsumerPhone();
        Integer distinct = this.isDistinct(consumerPhone, productId, sizeId);
        if (distinct > 0) {
            res.setCode(202);
            res.setMsg("结账单中已有相同属性的商品");
        } else {
            if (shopCartMapper.addShopCart(component) > 0) {
                res.setCode(200);
                res.setMsg("添加成功");
            } else {
                res.setCode(400);
                res.setMsg("添加失败");
            }
        }

        return res;
    }

    /**
     * 移除购物车
     *
     * @param id id
     * @return {@link Integer}
     */
    @Override
    public Res removeShopCart(Long id) {
        Res res = new Res();
        if (shopCartMapper.removeShopCart(id) > 0) {
            res.setCode(200);
        } else {
            res.setCode(400);
            res.setMsg("移除商品失败");
        }
        return res;
    }

    /**
     * 清空空购物车
     *
     * @param consumerPhone 消费者电话
     * @return {@link Integer}
     */
    @Override
    public Res emptyShopCart(String consumerPhone) {
        Res res = new Res();
        List<ShopCartView> shopCartViews = shopCartMapper.byConsumerPhoneGetShops(consumerPhone);
        if (shopCartViews.size() < 1) {
            res.setCode(201);
            res.setMsg("购物车内以无商品");
        }
        if (shopCartMapper.emptyShopCart(consumerPhone) > 0) {
            res.setCode(200);
            res.setMsg("清空购物车成功");
        } else {
            res.setCode(400);
            res.setMsg("清空购物车失败");
        }
        return res;
    }

    /**
     * 查重复的商品
     *
     * @param consumerPhone 消费者电话
     * @param productId     产品标识
     * @param sizeId        尺寸标识
     * @return {@link Integer}
     */
    @Override
    public Integer isDistinct(String consumerPhone, Long productId, Long sizeId) {
        return shopCartMapper.isDistinct(consumerPhone, productId, sizeId);
    }

    /**
     * 更新购物车
     *
     * @param data 数据
     * @return {@link Integer}
     */
    @Override
    public Res updateShopCart(ConsumerProductRelationshipRel data) {
        Res res = new Res();
        if (Objects.isNull(data)) {
            res.setCode(104);
            res.setMsg("未知数据");
            return res;
        } else {

            if (data.getProductQuantity() < 1) {
                res.setCode(103);
                res.setMsg("商品数量至少要大于0");
            } else {
                shopCartMapper.updateShopCart(data);
                res.setCode(200);
                res.setMsg("更新成功");
            }

            return res;
        }

    }

    @Override
    public Res updateSize(ConsumerProductRelationshipRel data) {
        Res res = new Res();
        if (Objects.isNull(data)) {
            res.setCode(104);
            res.setMsg("未知数据");
            return res;
        } else {
            if (Objects.isNull(data.getSizeId())) {
                res.setCode(103);
                res.setMsg("尺寸不能为空");
            } else {
                if (shopCartMapper.updateSize(data)>0) {
                    res.setCode(200);
                    res.setMsg("更新成功");
                }else {
                    res.setCode(400);
                    res.setMsg("更新失败");
                }

            }

            return res;
        }
    }


}
